<?php
class Utils
{
	/**
 * 写文件
 *
 * @param string $file
 * @param string $data
 * @return int
 */
	public static function writeFile($file, $data)
	{
		$dir = dirname($file);
		if (!is_dir($dir)) Helper_Folder::create($dir);

		$result = false;
		$fp = @fopen($file, 'w');
		if ($fp && @flock($fp, LOCK_EX))
		{
			$result = @fwrite($fp, $data);
			@flock($fp, LOCK_UN);
			@fclose($fp);
			@chmod($file, 0777);
		}

		return $result;
	}

	public static function stringEncode($data, $key)
	{
		$klen = strlen($key);
		$len = strlen($data);
		$args = array(str_repeat('v', $len+1), $len);
		while (--$len>=0) {
			$k = $len % $klen;
			$args[] = ord($data[$len]) ^ ord($key[$k]);
		}
		return base64_encode(call_user_func_array('pack', $args));
	}

	public static function stringDecode($data, $key)
	{
		$data = base64_decode($data);
		$len = unpack('vlen', $data);
		$len = $len['len'];
		$blen = strlen(pack('v', 0));
		$klen = strlen($key);
		$slen = strlen($data);
		$code = '';
		while(--$len>=0){
			$k = $len % $klen;
			$chr = unpack('vchr', substr($data, -(1+$len)*$blen, $blen));
			$chr = chr(intval($chr['chr']) ^ ord($key[$k]));
			$code = $chr.$code;
		}
		return $code;
	}

	/**
 * 读取缓存
 *
 * @param string $file
 * @param string $path
 * @param boolean $iscachevar
 * @return array
 */
	public static function cacheRead($file, $path = null, $iscachevar = 0)
	{
		if(!$path) $path = CACHE_PATH;
		$cachefile = $path.$file;
		if($iscachevar)
		{
			global $TEMP;
			$key = 'cache_'.substr($file, 0, -4);
			return isset($TEMP[$key]) ? $TEMP[$key] : $TEMP[$key] = @include $cachefile;
		}
		return @include $cachefile;
	}

	/**
 * 写入缓存
 *
 * @param string $file
 * @param array $array
 * @param string $path
 * @return int
 */
	public static function cacheWrite($file, $array, $path = null)
	{
		if(!is_array($array)) return false;
		$array = "<?php\nreturn ".var_export($array, true).";";
		$cachefile = ($path ? $path : CACHE_PATH).$file;
		$strlen = Utils::writeFile($cachefile, $array);
		return $strlen;
	}

	/**
 * 删除缓存文件
 *
 * @param string $file
 * @param string $path
 * @return boolean
 */
	public static function cacheDelete($file, $path = '')
	{
		$cachefile = ($path ? $path : CACHE_PATH).$file;
		return @unlink($cachefile);
	}
	
	public static function pages($total, $page = 1, $pagesize = 20, $offset = 2, $url = null, $mode = false)
	{
		if($total <= $pagesize) return '';
		$page = max(intval($page), 1);
		$pages = ceil($total/$pagesize);
		$page = min($pages, $page);
		$prepage = max($page-1, 1);
		$nextpage = min($page+1, $pages);
		$from = max($page - $offset, 2);
		if ($pages - $page - $offset < 1) $from = max($pages - $offset*2 - 1, 2);
		$to = min($page + $offset, $pages-1);
		if ($page - $offset < 2) $to = min($offset*2+2, $pages-1);
		$more = 1;
		if ($pages <= ($offset*2+5))
		{
			$from = 2;
			$to = $pages - 1;
			$more = 0;
		}
		$str = '';
		$str .= '<li><a href="'.pages_url($url, $prepage, $mode).'">上一页</a></li>';
		$str .= $page == 1 ? '<li><a href="'.pages_url($url, 1, $mode).'" class="now">1</a></li>' : '<li><a href="'.pages_url($url, 1, $mode).'">1'.($from > 2 && $more ? '...' : '').'</a></li>';
		if ($to >= $from)
		{
			for($i = $from; $i <= $to; $i++)
			{
				$str .= $i == $page ? '<li><a href="'.pages_url($url, $i, $mode).'" class="now">'.$i.'</a></li>' : '<li><a href="'.pages_url($url, $i, $mode).'">'.$i.'</a></li>';
			}
		}
		$str .= $page == $pages ? '<li><a href="'.pages_url($url, $pages, $mode).'" class="now">'.$pages.'</a></li>' : '<li><a href="'.pages_url($url, $pages, $mode).'">'.($to < $pages-1 && $more ? '...' : '').$pages.'</a></li>';
		$str .= '<li><a href="'.pages_url($url, $nextpage, $mode).'">下一页</a></li>';
		return $str;
	}

}